#include <iostream>
#include <string.h>
using namespace std;


void Test1()
{
    string s1("hello");
    s1 += " world";
    cout << s1 << endl;

    //读
    for (size_t i = 0; i < s1.size(); i++)
    {
        cout << s1[i] << " ";
    }

    //写
    for (size_t i = 0; i < s1.size(); i++)
    {
        s1[i] += 1;
    }

    //迭代器遍历
    cout <<"\n迭代器"<<endl;
    string::iterator it = s1.begin();
    while (it != s1.end())
    {
        cout << *it << " " ;
        ++it;
    }

    //反向迭代器遍历
    cout <<"\n反向迭代器"<<endl;
    string::reverse_iterator rit = s1.rbegin();
    while (rit != s1.rend())
    {
        *rit -= 1;
        cout << *rit << " " ;
        ++rit;
    }

    //C++11支持范围for————原理被替换成迭代器
    cout <<"\n范围for"<<endl;
    for (auto s : s1)
    {
        cout << s << " ";
    }

    s1.reserve(20);
    cout << s1.capacity() << endl;
    s1.resize(25,'a');
    cout << s1.capacity() << endl;
    cout << s1.size() << endl;
    cout << s1 << endl;

    string s2("123456");
    s2.insert(s2.begin(),'0');
    cout << s2 << endl;
    s2.insert(3,"444");
    cout << s2 << endl;
    s2.erase(2, 3);
    cout << s2 << endl;
}

// 写成打印分离url的函数
void split_url(const string &url)
{
    size_t u1 = url.find("://");
    if (u1 != string::npos)
        cout << url.substr(0, u1) << endl; 

    size_t u2 = url.find('/', u1 + 3);
    if (u2 != string::npos)
        cout << url.substr(u1 + 3, u2 - (u1 + 3)) << endl; 

    cout << url.substr(u2 + 1) << endl;
}

void Test2(){
    // find函数,查找
    string s1("string.cpp");
    string s2("string.cc");
    string s3("string.txt");

    size_t pos1 = s1.find('.');
    // substr函数，切割子串
    if (pos1 != string::npos)
        cout << s1.substr(pos1) << endl;
    ;

    size_t pos2 = s2.find('.');
    if (pos2 != string::npos)
        cout << s2.substr(pos2) << endl;
    ;

    size_t pos3 = s2.find('.');
    if (pos3 != string::npos)
        cout << s3.substr(pos3) << endl;
    ;

    // rfind函数,倒着查找
    string s4("string.tar.zip");
    size_t pos4 = s4.rfind('.');
    if (pos4 != string::npos)
        cout << s4.substr(pos4);

    // 网址切割————协议 域名 资源名称
    string url("https://gitee.com/kamijou0");
    size_t u1 = url.find("://");
    if (u1 != string::npos)
        cout << url.substr(0, u1) << endl; // https

    size_t u2 = url.find('/', u1 + 3);
    if (u2 != string::npos)
        cout << url.substr(u1 + 3, u2 - (u1 + 3)) << endl; // gitee.com

    cout << url.substr(u2 + 1) << endl; // kamijou0

    //将上述代码封装成一个函数
    string github_url("https://github.com/kamijou0");
    split_url(github_url);
}

//比较大小
void Test3(){
    string s1("hello");
    string s2("world");
    cout << (s1 < s2) << endl;
    cout << (s1 < "world") << endl;
    cout << ("hello" < "world") << endl;
}

//getline
void Test4(){
	string s;
	//cin >> s;//换行或空格
	getline(cin,s);//读取一行

	size_t pos = s.rfind(' ');
	cout << pos << endl; 
}

int main(){
    //Test1();
    //Test2();
    //Test3();
    Test4();
    return 0;
}